<template>
  	<div id="name">
    	<div class="wl-body">
      		<div class="top"></div>
      		<div class="topbox slideOut">
        		<div class="bdds">
          			<div class="link-item">
            			<div class="zc-dj t-left">USDT</div>
						<div class="slink defcf">
							<div class="flex1">{{ $t("m.ky") }}</div>
							<div class="flex1">{{ $t("m.zh") }}({{ bzdw }})</div>
						</div>
            			<div class="slink u-dj">
							<div class="flex1">
								{{ see ? (s ? s.trxAmount.toFixed(4) : "0") : "*****" }}
							</div>
							<div class="flex1">
								{{
								see ? (s ? (s.trxAmount * utoc).toFixed(2) : "0") : "*****"
								}}
							</div>
            			</div>
          			</div>
        		</div>
      		</div>
      		<div>
        		<div class="i-title t-left mtp">— {{ $t("m.cwjl") }} —</div>
        		<div class="mtp slink w30 xuankuang">
					<div class="xk1l scllow" :class="[acchange == 0 ? 'active' : '']" @click="changers(0)" >
            			{{ $t("m.qb") }}
          			</div>
          			<div class="xk1 scllow" :class="[acchange == 1 ? 'active' : '']" @click="changers(1)" >
            			{{ $t("m.cb") }}
          			</div>
					<div class="xk1r scllow" :class="[acchange == 2 ? 'active' : '']" @click="changers(2)" >
						{{ $t("m.tb") }}
					</div>
          			<!-- <div class="xk1" :class="[acchange==3?'active':'']" @click="changers(3)">闪兑</div> -->
        		</div>
        		<div class="bdds slideIn czlist">
          			<van-list v-model="loading" :finished="finished" :finished-text="$t('m.nomore')" @load="onLoad" >
            			<div class="link-item bottml" v-for="(item, k) in lslist" :key="k">
              				<div class="zc-dj t-left">
                				{{ item.state == 1 ? $t("m.cb") : item.state == 2 ? $t("m.tb") : item.state == 3 ? $t("m.sd") : "" }}
              				</div>
							<div class="slink defcf">
								<div class="flex1">{{ $t("m.sl") }}</div>
								<div class="flex1">{{ $t("m.zt") }}</div>
								<div class="flex1">{{ $t("m.sj") }}</div>
							</div>
              				<div class="slink u-dj">
                				<div class="flex1">{{ item.amount }}</div>
               	 				<div class="flex1">
                  					{{ item.status == 1 ? $t("m.qkqrz") : item.status == 2 ? $t("m.ywc") : item.status == 3 ? $t("m.qkqrsb") : "" }}
                				</div>
                				<div class="flex1">{{ item.createTime }}</div>
              				</div>
            			</div>
          			</van-list>
        		</div>
      		</div>
      		<div class="bottomflex flesa">
        		<div class="sxdq mtp" @click="congbi()">
          			<img src="../assets/timg/cbbtn.png" alt="" class="iixo" />
					<div>
						{{ $t("m.cb") }}
					</div>
        		</div>
				<div class="sxdq mtp" @click="tibi()">
					<img src="../assets/timg/tbbtn.png" alt="" class="iixo" />
					<div>
						{{ $t("m.tb") }}
					</div>
				</div>
        		<!-- <div class="sxdq mtp" @click="goto('bzdh')">
          			<img src="../assets/timg/sdbtn.png" alt="" class="iixo" />
					<div>
						{{ $t("m.sd") }}
					</div>
        		</div> -->
      		</div>
    	</div>
    	<van-popup v-model="cbshow" position="bottom">
      		<div class="tbdlog">
        		<div class="sdadw">
          			<div class="txte">{{ $t("m.cb") }}</div>
          			<div>
            			<div class="tts">USDT</div>
            			<div class="isde">
							<input
								type="number"
								class="inputs"
								v-model="nums"
								:placeholder="$t('m.zxtbsl') + min"
								@keydown="gs"
							/>
            			</div>
						<div class="isdesd nos">
							<div>{{ $t("m.kcz") }}:</div>
							<div>{{ unums ? unums : "***" }} USDT</div>
						</div>
          			</div>
        		</div>
        		<div class="bnts" @click="yqmao()">{{ $t("m.cb") }}</div>
      		</div>
    	</van-popup>
    	<van-popup v-model="tbshow" position="bottom">
      		<div class="tbdlog">
        		<div class="sdadw">
          			<div class="txte">{{ $t("m.tb") }}</div>
          			<div>
            			<div class="tts">USDT</div>
            			<div class="isdesd bottmb">
              				<div>
								<input type="number" class="inputs" v-model="tbnums" :placeholder="$t('m.zxtbsl') + min" @keydown="gs" />
              				</div>
              				<div>
                				<span @click="all()">{{ $t("m.qb") }}</span>
              				</div>
            			</div>
						<div class="isdesd">
							<div>{{ $t("m.ky") }}:</div>
							<div>{{ s ? s.trxAmount.toFixed(4) : "0" }} USDT</div>
						</div>
            			<div class="isdesd">
							<div>{{ $t("m.kgf") }}：</div>
							<div>{{ sxf }}USDT/{{ $t("m.bi") }}</div>
            			</div>
						<div class="isde">
							<div>{{ $t("m.dzsl") }}:</div>
							<div>{{ tbnums ? (tbnums - sxf).toFixed(4) : 0 }} USDT</div>
						</div>
          			</div>
        		</div>
        		<div class="bnts" @click="yqmaos()">{{ $t("m.tb") }}</div>
      		</div>
    	</van-popup>
  	</div>
</template>

<script>
import { recharge, tokenBalance, getSign } from "../api/tron";

export default {
  name: "name",
  data() {
    return {
      isPublish: false,
      acchange: 0,
      cbshow: false,
      tbshow: false,
      show: true,
      utoc: this.$store.getters.utoc,
      ttoc: this.$store.getters.ttoc,
      see: "",
      min: "",
      lslist: [],
      loading: false,
      finished: false,
      pageNum: 1,
      nums: "",
      unums: "",
      suoding: false,
      tbnums: "",
      tbsding: false,
      bzdw: "CNY",
      gsb: "",
      sxf: "",
    };
  },

  created() {
    this.gb();
    if (this.$store.getters.language == "en-US") {
      this.bzdw = "USD";
    } else {
      this.bzdw = "CNY";
    }
    if (this.$store.getters.see == "0") {
      this.see = false;
    } else {
      this.see = true;
    }
  },
  computed: {
    s() {
      return this.$store.getters.s;
    },
  },

  methods: {
    gs(e) {
      e.target.value = e.target.value.match(/^\d*(\.?\d{0,5})/g)[0] || null;
    },
    gb() {
      let datas = {
        params: {
          pageNum: 1,
          pageSize: 10,
        },
      };
      this.$post({
        url: "sms-base/get",
        data: datas,
        callback: (data) => {
          this.$nextTick(function () {
            if (data.data) {
              this.gsb = data.data;
              this.min = this.gsb.minU||0;
            }
          });
        },
      });
    },
    guanbi() {
      this.$router.go(-1);
    },
    changers(val) {
      this.acchange = val;
      this.show = false;
      this.lslist = [];
      this.pageNum = 1;
      this.onLoad();
    },

    congbi() {
      this.cbshow = true;
      this.getye();
    },
    async getye() {
      await tokenBalance("USDT")
        .then((res) => {
          this.unums = res;
        })
        .catch(() => {
          this.getye();
        });
    },
    yqmao() {
		if (this.nums == 0) {
			this.$toast(this.$i18n.t("m.dhslshr"));
			return;
		}
		if (this.nums > this.unums) {
			this.$toast(this.$i18n.t("m.qbyebz"));
			this.nums = this.unms;
			return;
		}

      	if (this.suoding) {
        	return;
      	} else {
			this.$toast.loading();
			this.suoding = true;
        	recharge("USDT", this.nums).then((res) => {
      
        if(res){
            this.$toast(this.$i18n.t("m.qxcz"))
         }else{
            // 充值成功
            console.log(res);
            this.getdata();
            this.suoding = false;
             }


          	},(err) => {
				// 交易失败
				// this.$toast('交易失败'+err);
				console.log(err);
				this.$toast(this.$i18n.t("m.messageerrors"));
				this.suoding = false;
          	});
      	}
    },
    tibi() {
      this.tbshow = true;
      this.sxf = this.gsb.feeU||0;
    },
    goto(val) {
      this.$router.push({ path: "/" + val });
    },

    all() {
      this.tbnums = this.s.trxAmount;
    },
    yqmaos() {
      // if (!this.sxf) {
      //   this.$toast(this.$i18n.t("m.sjzjz"));
      //   return;
      // }
      if (this.tbnums == 0) {
        this.$toast(this.$i18n.t("m.dhslshr"));
        return;
      }
      if (this.tbnums - this.sxf <= 0) {
        this.$toast(this.$i18n.t("m.dhslsh"));
        return;
      }
      if (this.s.trxAmount - this.tbnums < 0) {
        this.tbnums = this.s.trxAmount;
        this.$toast(this.$i18n.t("m.qbyebz"));
        return;
      }
      if (this.tbnums - this.min <= 0) {
        this.$toast(this.$i18n.t("m.zxtbsl") + this.min);
        return;
      }
      if (this.gsb.transferState == 1) {
        this.$toast(this.$i18n.t("m.qukuaigengxin"));
        return;
      } else {
        if (this.tbnums <= 0 || this.tbsding) {
          this.$toast(this.$i18n.t("m.messageerror"));
          return;
        } else {
          this.$dialog
            .confirm({
              title: this.$i18n.t("m.txt"),
              message: this.$i18n.t("m.tb") + (this.tbnums - this.sxf) + "USDT",
              confirmButtonText: this.$i18n.t("m.qding"),
              cancelButtonText: this.$i18n.t("m.gb"),
            })
            .then(() => {
              getSign(this.$store.getters.userWallet).then(
                (res) => {
                  if (res) {
                    this.tbsding = true;
                    let datas = {
                      sendAddress: this.$store.getters.userWallet,
                      sendId: this.$store.getters.nameid,
                      receiveAddress: this.$store.getters.userWallet,
                      state: 2,
                      amount: parseFloat(this.tbnums).toFixed(4),
                      type: 1,
                    };
                    this.$post({
                      url: "wms-wallet/transferWallet",
                      data: datas,
                      callback: (data) => {
                        this.$nextTick(function () {
                          if (data.data) {
                            if (this.$store.getters.language == "zh-CN") {
                              this.$toast(data.message);
                            } else {
                              this.$toast(this.$i18n.t("m.messagesuccess"));
                            }
                            this.tbnums = "";
                            this.getdata();
                            this.tbshow = false;
                            setTimeout(() => {
                              this.tbsding = false;
                            }, 2000);
                          } else {
                            if (this.$store.getters.language == "zh-CN") {
                              this.$toast(data.message);
                            } else {
                              this.$toast(this.$i18n.t("m.messageerror"));
                            }
                            setTimeout(() => {
                              this.tbsding = false;
                            }, 2000);
                          }
                        });
                      },
                    });
                  } else {
                    this.$toast(this.$i18n.t("m.messageerrors"));
                    this.tbsding = false;
                    this.tbnums = "";
                  }
                },
                () => {
                  this.$toast(this.$i18n.t("m.messageerrors"));
                }
              );
            });
        }
      }
    },
    getdata() {
      let datas = {
        params: {
          address: this.$store.getters.userWallet,
        },
      };
      this.$get({
        url: "wms-wallet/getWallet",
        data: datas,
        callback: (data) => {
          this.$nextTick(function () {
            this.s = data.data;
            if (this.s) {
              this.$toast(this.$i18n.t("m.messagesuccess"));
              this.cbshow = false;
              this.nums = "";
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
    onLoad() {
      if (this.$store.getters.nameid) {
        var datas = {
            uid: this.$store.getters.nameid,
            pageSize: 10,
            pageNum: this.pageNum,
            type: 1,
            state: this.acchange,
        };
        this.$get({
          url: "wms-wallet/getTransferHistory",
          data: datas,
          callback: (data) => {
            this.$nextTick(function () {
              var list = data.data;
              if (list) {
                this.show = true;
                if (this.pageNum > 1) {
                  this.lslist = [...this.lslist, ...list];
                } else {
                  this.lslist = list;
                }
                this.loading = false;
                if (list.length != 0) {
                  this.pageNum++;
                } else {
                  this.finished = true;
                }
              }
            });
          },
        }).catch(() => {
          this.$toast(this.$i18n.t("m.errordata"));
        });
      } else {
        this.getuid();
      }
    },
    getuid() {
      this.$get({
        url: "wms-wallet/getWallet",
        data: { address: this.$store.getters.userWallet},
        callback: (data) => {
          this.$nextTick(function () {
            if (data.data) {
              this.$store.commit("nameid", data.data.id);
              this.onLoad();
            }
          });
        },
      }).catch(() => {
        this.$toast(this.$i18n.t("m.errordata"));
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>